package javolution.util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import javax.realtime.MemoryArea;
import javolution.context.ObjectFactory;
import javolution.context.PersistentContext;
import javolution.lang.Reusable;
import javolution.util.FastCollection;

/* loaded from: classes2.dex */
public class FastList<E> extends FastCollection<E> implements List<E>, Reusable {
    private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastList.1
        @Override // javolution.context.ObjectFactory
        public void cleanup(Object obj) {
            ((FastList) obj).reset();
        }

        @Override // javolution.context.ObjectFactory
        public Object create() {
            return new FastList();
        }
    };
    static volatile int ONE_VOLATILE = 1;
    private static final long serialVersionUID = 1;
    private transient Node<E> _head;
    private transient int _size;
    private transient Node<E> _tail;
    private transient FastComparator<? super E> _valueComparator;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static final class FastListIterator implements ListIterator {
        private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastList.FastListIterator.1
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public void cleanup(Object obj) {
                FastListIterator fastListIterator = (FastListIterator) obj;
                fastListIterator._list = null;
                fastListIterator._currentNode = null;
                fastListIterator._nextNode = null;
            }

            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new FastListIterator();
            }
        };
        private Node _currentNode;
        private int _length;
        private FastList _list;
        private int _nextIndex;
        private Node _nextNode;

        private FastListIterator() {
        }

        public static FastListIterator valueOf(FastList fastList, Node node, int i, int i2) {
            FastListIterator fastListIterator = (FastListIterator) FACTORY.object();
            fastListIterator._list = fastList;
            fastListIterator._nextNode = node;
            fastListIterator._nextIndex = i;
            fastListIterator._length = i2;
            return fastListIterator;
        }

        @Override // java.util.ListIterator
        public void add(Object obj) {
            this._list.addBefore(this._nextNode, obj);
            this._currentNode = null;
            this._length++;
            this._nextIndex++;
        }

        @Override // java.util.ListIterator, java.util.Iterator
        public boolean hasNext() {
            return this._nextIndex != this._length;
        }

        @Override // java.util.ListIterator
        public boolean hasPrevious() {
            return this._nextIndex != 0;
        }

        @Override // java.util.ListIterator, java.util.Iterator
        public Object next() {
            int i = this._nextIndex;
            if (i != this._length) {
                this._nextIndex = i + 1;
                Node node = this._nextNode;
                this._currentNode = node;
                this._nextNode = node._next;
                return this._currentNode._value;
            }
            throw new NoSuchElementException();
        }

        @Override // java.util.ListIterator
        public int nextIndex() {
            return this._nextIndex;
        }

        @Override // java.util.ListIterator
        public Object previous() {
            int i = this._nextIndex;
            if (i != 0) {
                this._nextIndex = i - 1;
                Node node = this._nextNode._previous;
                this._nextNode = node;
                this._currentNode = node;
                return this._currentNode._value;
            }
            throw new NoSuchElementException();
        }

        @Override // java.util.ListIterator
        public int previousIndex() {
            return this._nextIndex - 1;
        }

        @Override // java.util.ListIterator, java.util.Iterator
        public void remove() {
            Node node = this._currentNode;
            if (node == null) {
                throw new IllegalStateException();
            }
            Node node2 = this._nextNode;
            if (node2 == node) {
                this._nextNode = node2._next;
            } else {
                this._nextIndex--;
            }
            this._list.delete(this._currentNode);
            this._currentNode = null;
            this._length--;
        }

        @Override // java.util.ListIterator
        public void set(Object obj) {
            Node node = this._currentNode;
            if (node == null) {
                throw new IllegalStateException();
            }
            node._value = obj;
        }
    }

    /* loaded from: classes2.dex */
    public static class Node<E> implements FastCollection.Record, Serializable {
        private Node<E> _next;
        private Node<E> _previous;
        private E _value;

        protected Node() {
        }

        @Override // javolution.util.FastCollection.Record
        public final Node<E> getNext() {
            return this._next;
        }

        @Override // javolution.util.FastCollection.Record
        public final Node<E> getPrevious() {
            return this._previous;
        }

        public final E getValue() {
            return this._value;
        }
    }

    /* loaded from: classes2.dex */
    private static final class SubList extends FastCollection implements List, Serializable {
        private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastList.SubList.1
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public void cleanup(Object obj) {
                SubList subList = (SubList) obj;
                subList._list = null;
                subList._head = null;
                subList._tail = null;
            }

            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new SubList();
            }
        };
        private Node _head;
        private FastList _list;
        private int _size;
        private Node _tail;

        private SubList() {
        }

        private final Node nodeAt(int i) {
            int i2 = this._size;
            if (i <= (i2 >> 1)) {
                Node node = this._head;
                while (true) {
                    int i3 = i - 1;
                    if (i < 0) {
                        return node;
                    }
                    node = node._next;
                    i = i3;
                }
            } else {
                Node node2 = this._tail;
                int i4 = i2 - i;
                while (true) {
                    int i5 = i4 - 1;
                    if (i4 <= 0) {
                        return node2;
                    }
                    node2 = node2._previous;
                    i4 = i5;
                }
            }
        }

        public static SubList valueOf(FastList fastList, Node node, Node node2, int i) {
            SubList subList = (SubList) FACTORY.object();
            subList._list = fastList;
            subList._head = node;
            subList._tail = node2;
            subList._size = i;
            return subList;
        }

        @Override // java.util.List
        public void add(int i, Object obj) {
            if (i >= 0 && i <= this._size) {
                this._list.addBefore(nodeAt(i), obj);
                return;
            }
            throw new IndexOutOfBoundsException("index: " + i);
        }

        @Override // java.util.List
        public boolean addAll(int i, Collection collection) {
            if (i < 0 || i > this._size) {
                throw new IndexOutOfBoundsException("index: " + i);
            }
            Node<E> nodeAt = nodeAt(i);
            Iterator<E> it = collection.iterator();
            while (it.hasNext()) {
                this._list.addBefore(nodeAt, it.next());
            }
            return collection.size() != 0;
        }

        @Override // javolution.util.FastCollection
        public void delete(FastCollection.Record record) {
            this._list.delete(record);
        }

        @Override // java.util.List
        public Object get(int i) {
            if (i < 0 || i >= this._size) {
                throw new IndexOutOfBoundsException("index: " + i);
            }
            return nodeAt(i)._value;
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record head() {
            return this._head;
        }

        @Override // java.util.List
        public int indexOf(Object obj) {
            FastComparator<? super E> valueComparator = this._list.getValueComparator();
            Node node = this._head;
            Node node2 = this._tail;
            int i = 0;
            while (true) {
                node = node._next;
                if (node == node2) {
                    return -1;
                }
                if (valueComparator.areEqual(obj, (Object) node._value)) {
                    return i;
                }
                i++;
            }
        }

        @Override // java.util.List
        public int lastIndexOf(Object obj) {
            FastComparator<? super E> valueComparator = getValueComparator();
            int size = size() - 1;
            Node node = this._tail;
            Node node2 = this._head;
            while (true) {
                node = node._previous;
                if (node == node2) {
                    return -1;
                }
                if (valueComparator.areEqual(obj, (Object) node._value)) {
                    return size;
                }
                size--;
            }
        }

        @Override // java.util.List
        public ListIterator listIterator() {
            return listIterator(0);
        }

        @Override // java.util.List
        public ListIterator listIterator(int i) {
            if (i < 0 || i > this._size) {
                throw new IndexOutOfBoundsException("index: " + i + " for list of size: " + this._size);
            }
            return FastListIterator.valueOf(this._list, nodeAt(i), i, this._size);
        }

        @Override // java.util.List
        public Object remove(int i) {
            if (i < 0 || i >= this._size) {
                throw new IndexOutOfBoundsException("index: " + i);
            }
            Node nodeAt = nodeAt(i);
            Object obj = nodeAt._value;
            this._list.delete(nodeAt);
            return obj;
        }

        @Override // java.util.List
        public Object set(int i, Object obj) {
            if (i < 0 || i >= this._size) {
                throw new IndexOutOfBoundsException("index: " + i);
            }
            Node nodeAt = nodeAt(i);
            Object obj2 = nodeAt._value;
            nodeAt._value = obj;
            return obj2;
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public int size() {
            return this._size;
        }

        @Override // java.util.List
        public List subList(int i, int i2) {
            if (i < 0 || i2 > this._size || i > i2) {
                throw new IndexOutOfBoundsException("fromIndex: " + i + ", toIndex: " + i2 + " for list of size: " + this._size);
            }
            return valueOf(this._list, nodeAt(i)._previous, nodeAt(i2), i2 - i);
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record tail() {
            return this._tail;
        }

        @Override // javolution.util.FastCollection
        public Object valueOf(FastCollection.Record record) {
            return this._list.valueOf(record);
        }
    }

    public FastList() {
        this(4);
    }

    public FastList(int i) {
        this._head = newNode();
        this._tail = newNode();
        this._valueComparator = FastComparator.DEFAULT;
        ((Node) this._head)._next = this._tail;
        ((Node) this._tail)._previous = this._head;
        Node<E> node = this._tail;
        int i2 = 0;
        while (true) {
            int i3 = i2 + 1;
            if (i2 >= i) {
                return;
            }
            Node<E> newNode = newNode();
            ((Node) newNode)._previous = node;
            ((Node) node)._next = newNode;
            node = newNode;
            i2 = i3;
        }
    }

    public FastList(String str) {
        this();
        new PersistentContext.Reference(str, this) { // from class: javolution.util.FastList.2
            @Override // javolution.context.PersistentContext.Reference
            protected void notifyChange() {
                FastList.this.clear();
                FastList.this.addAll((Collection) ((FastList) get()));
            }
        };
    }

    public FastList(Collection<? extends E> collection) {
        this(collection.size());
        addAll(collection);
    }

    private static boolean defaultEquals(Object obj, Object obj2) {
        if (obj == null) {
            if (obj2 != null) {
                return false;
            }
        } else if (obj != obj2 && !obj.equals(obj2)) {
            return false;
        }
        return true;
    }

    private void increaseCapacity() {
        MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastList.3
            @Override // java.lang.Runnable
            public void run() {
                Node<E> newNode = FastList.this.newNode();
                FastList.this._tail._next = newNode;
                ((Node) newNode)._previous = FastList.this._tail;
                Node<E> newNode2 = FastList.this.newNode();
                ((Node) newNode)._next = newNode2;
                ((Node) newNode2)._previous = newNode;
                Node<E> newNode3 = FastList.this.newNode();
                ((Node) newNode2)._next = newNode3;
                ((Node) newNode3)._previous = newNode2;
                Node<E> newNode4 = FastList.this.newNode();
                ((Node) newNode3)._next = newNode4;
                ((Node) newNode4)._previous = newNode3;
            }
        });
    }

    public static <E> FastList<E> newInstance() {
        return (FastList) FACTORY.object();
    }

    private final Node<E> nodeAt(int i) {
        Node<E> node = this._head;
        while (true) {
            int i2 = i - 1;
            if (i < 0) {
                return node;
            }
            node = ((Node) node)._next;
            i = i2;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        this._head = new Node<>();
        this._tail = new Node<>();
        ((Node) this._head)._next = this._tail;
        ((Node) this._tail)._previous = this._head;
        setValueComparator((FastComparator) objectInputStream.readObject());
        int readInt = objectInputStream.readInt();
        while (true) {
            int i = readInt - 1;
            if (readInt == 0) {
                return;
            }
            addLast(objectInputStream.readObject());
            readInt = i;
        }
    }

    public static void recycle(FastList fastList) {
        FACTORY.recycle(fastList);
    }

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.writeObject(getValueComparator());
        objectOutputStream.writeInt(this._size);
        Node<E> node = this._head;
        int i = this._size;
        while (true) {
            int i2 = i - 1;
            if (i == 0) {
                return;
            }
            node = ((Node) node)._next;
            objectOutputStream.writeObject(((Node) node)._value);
            i = i2;
        }
    }

    @Override // java.util.List
    public final void add(int i, E e) {
        if (i >= 0 && i <= this._size) {
            addBefore(nodeAt(i), e);
            return;
        }
        throw new IndexOutOfBoundsException("index: " + i);
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final boolean add(E e) {
        addLast(e);
        return true;
    }

    @Override // java.util.List
    public final boolean addAll(int i, Collection<? extends E> collection) {
        if (i < 0 || i > this._size) {
            throw new IndexOutOfBoundsException("index: " + i);
        }
        Node<E> nodeAt = nodeAt(i);
        for (E e : collection) {
            addBefore(nodeAt, e);
        }
        return collection.size() != 0;
    }

    public final void addBefore(Node<E> node, E e) {
        if (((Node) this._tail)._next == null) {
            increaseCapacity();
        }
        Node node2 = ((Node) this._tail)._next;
        Node node3 = ((Node) this._tail)._next = node2._next;
        if (node3 != null) {
            node3._previous = this._tail;
        }
        Node node4 = ((Node) node)._previous;
        node4._next = node2;
        ((Node) node)._previous = node2;
        node2._next = node;
        node2._previous = node4;
        node2._value = e;
        this._size += ONE_VOLATILE;
    }

    public final void addFirst(E e) {
        addBefore(((Node) this._head)._next, e);
    }

    public void addLast(E e) {
        if (((Node) this._tail)._next == null) {
            increaseCapacity();
        }
        ((Node) this._tail)._value = e;
        this._tail = ((Node) this._tail)._next;
        this._size += ONE_VOLATILE;
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final void clear() {
        this._size = ONE_VOLATILE - 1;
        Node<E> node = this._head;
        Node<E> node2 = this._tail;
        while (true) {
            node = ((Node) node)._next;
            if (node == node2) {
                this._tail = ((Node) this._head)._next;
                return;
            }
            ((Node) node)._value = null;
        }
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final boolean contains(Object obj) {
        return indexOf(obj) >= 0;
    }

    @Override // javolution.util.FastCollection
    public final void delete(FastCollection.Record record) {
        Node node = (Node) record;
        this._size -= ONE_VOLATILE;
        node._value = null;
        node._previous._next = node._next;
        node._next._previous = node._previous;
        Node node2 = ((Node) this._tail)._next;
        node._previous = this._tail;
        node._next = node2;
        ((Node) this._tail)._next = node;
        if (node2 != null) {
            node2._previous = node;
        }
    }

    @Override // java.util.List
    public final E get(int i) {
        if (i < 0 || i >= this._size) {
            throw new IndexOutOfBoundsException("index: " + i);
        }
        return (E) ((Node) nodeAt(i))._value;
    }

    public final E getFirst() {
        Node<E> node = ((Node) this._head)._next;
        if (node != this._tail) {
            return (E) ((Node) node)._value;
        }
        throw new NoSuchElementException();
    }

    public final E getLast() {
        Node<E> node = ((Node) this._tail)._previous;
        if (node != this._head) {
            return (E) ((Node) node)._value;
        }
        throw new NoSuchElementException();
    }

    @Override // javolution.util.FastCollection
    public FastComparator<? super E> getValueComparator() {
        return this._valueComparator;
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public int hashCode() {
        FastComparator<? super E> valueComparator = getValueComparator();
        Node<E> node = this._head;
        Node<E> node2 = this._tail;
        int i = 1;
        while (true) {
            node = ((Node) node)._next;
            if (node == node2) {
                return i;
            }
            i = (i * 31) + valueComparator.hashCodeOf((Object) ((Node) node)._value);
        }
    }

    @Override // javolution.util.FastCollection
    public final Node<E> head() {
        return this._head;
    }

    /* JADX WARN: Code restructure failed: missing block: B:12:0x0028, code lost:
        return r3;
     */
    @Override // java.util.List
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public final int indexOf(java.lang.Object r6) {
        /*
            r5 = this;
            javolution.util.FastComparator r0 = r5.getValueComparator()
            javolution.util.FastList$Node<E> r1 = r5._head
            javolution.util.FastList$Node<E> r2 = r5._tail
            r3 = 0
        L9:
            javolution.util.FastList$Node r1 = javolution.util.FastList.Node.access$000(r1)
            if (r1 == r2) goto L2c
            javolution.util.FastComparator<java.lang.Object> r4 = javolution.util.FastComparator.DEFAULT
            if (r0 != r4) goto L1e
            java.lang.Object r4 = javolution.util.FastList.Node.access$200(r1)
            boolean r4 = defaultEquals(r6, r4)
            if (r4 == 0) goto L29
            goto L28
        L1e:
            java.lang.Object r4 = javolution.util.FastList.Node.access$200(r1)
            boolean r4 = r0.areEqual(r6, r4)
            if (r4 == 0) goto L29
        L28:
            return r3
        L29:
            int r3 = r3 + 1
            goto L9
        L2c:
            r6 = -1
            return r6
        */
        throw new UnsupportedOperationException("Method not decompiled: javolution.util.FastList.indexOf(java.lang.Object):int");
    }

    @Override // javolution.util.FastCollection, java.util.Collection, java.lang.Iterable
    public Iterator<E> iterator() {
        return listIterator();
    }

    @Override // java.util.List
    public final int lastIndexOf(Object obj) {
        FastComparator<? super E> valueComparator = getValueComparator();
        int size = size() - 1;
        Node<E> node = this._tail;
        Node<E> node2 = this._head;
        while (true) {
            node = ((Node) node)._previous;
            if (node == node2) {
                return -1;
            }
            if (valueComparator == FastComparator.DEFAULT) {
                if (defaultEquals(obj, ((Node) node)._value)) {
                    break;
                }
                size--;
            } else if (valueComparator.areEqual(obj, (Object) ((Node) node)._value)) {
                break;
            } else {
                size--;
            }
        }
        return size;
    }

    @Override // java.util.List
    public ListIterator<E> listIterator() {
        return FastListIterator.valueOf(this, ((Node) this._head)._next, 0, this._size);
    }

    @Override // java.util.List
    public ListIterator<E> listIterator(int i) {
        if (i < 0 || i > this._size) {
            throw new IndexOutOfBoundsException("index: " + i);
        }
        return FastListIterator.valueOf(this, nodeAt(i), i, this._size);
    }

    protected Node<E> newNode() {
        return new Node<>();
    }

    @Override // java.util.List
    public final E remove(int i) {
        if (i < 0 || i >= this._size) {
            throw new IndexOutOfBoundsException("index: " + i);
        }
        Node<E> nodeAt = nodeAt(i);
        E e = (E) ((Node) nodeAt)._value;
        delete(nodeAt);
        return e;
    }

    public final E removeFirst() {
        Node<E> node = ((Node) this._head)._next;
        if (node != this._tail) {
            E e = (E) ((Node) node)._value;
            delete(node);
            return e;
        }
        throw new NoSuchElementException();
    }

    public final E removeLast() {
        int i = this._size;
        if (i != 0) {
            this._size = i - ONE_VOLATILE;
            Node<E> node = ((Node) this._tail)._previous;
            E e = (E) ((Node) node)._value;
            this._tail = node;
            ((Node) node)._value = null;
            return e;
        }
        throw new NoSuchElementException();
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        clear();
        this._valueComparator = FastComparator.DEFAULT;
    }

    @Override // java.util.List
    public final E set(int i, E e) {
        if (i < 0 || i >= this._size) {
            throw new IndexOutOfBoundsException("index: " + i);
        }
        Node<E> nodeAt = nodeAt(i);
        E e2 = (E) ((Node) nodeAt)._value;
        ((Node) nodeAt)._value = e;
        return e2;
    }

    public FastList<E> setValueComparator(FastComparator<? super E> fastComparator) {
        this._valueComparator = fastComparator;
        return this;
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final int size() {
        return this._size;
    }

    @Override // java.util.List
    public final List<E> subList(int i, int i2) {
        if (i < 0 || i2 > this._size || i > i2) {
            throw new IndexOutOfBoundsException("fromIndex: " + i + ", toIndex: " + i2 + " for list of size: " + this._size);
        }
        return SubList.valueOf(this, ((Node) nodeAt(i))._previous, nodeAt(i2), i2 - i);
    }

    @Override // javolution.util.FastCollection
    public final Node<E> tail() {
        return this._tail;
    }

    @Override // javolution.util.FastCollection
    public List<E> unmodifiable() {
        return (List) super.unmodifiable();
    }

    @Override // javolution.util.FastCollection
    public final E valueOf(FastCollection.Record record) {
        return (E) ((Node) record)._value;
    }
}
